package com.kma.ncpractice2013.facade;

import javax.mail.Message;
import javax.mail.PasswordAuthentication;
import javax.mail.Session;
import javax.mail.Transport;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeMessage;
import java.util.Properties;

/**
  * Author: Katya Bileka
 * Date: 14.10.13
 * Time: 18:05
 */
public class MailFacadeImpl implements MailFacade {

	public static String home = "http://apptest1.elasticbeanstalk.com/";

    @Override
    public void sendLetter(String to, String subject, String textOfLetter) throws Exception {
        /*InitialContext ctx = new InitialContext();
        Session session = (Session) ctx.lookup("jndi/mail");*/


	    Properties props = new Properties();
	    props.put("mail.smtp.host", "smtp.gmail.com");
	    props.put("mail.smtp.socketFactory.port", "465");
	    props.put("mail.smtp.socketFactory.class",
	              "javax.net.ssl.SSLSocketFactory");
	    props.put("mail.smtp.auth", "true");
	    props.put("mail.smtp.port", "465");

	    Session session = Session.getInstance(props,
	                                                 new javax.mail.Authenticator() {
		                                                 protected PasswordAuthentication getPasswordAuthentication() {
			                                                 return new PasswordAuthentication("mailsender.res.system@gmail.com","xudof-kagas");
		                                                 }
	                                                 });



	    Message msg = new MimeMessage(session);
        InternetAddress from = new InternetAddress("mailsender.res.system@gmail.com");
        msg.setSubject(subject);
        msg.setRecipient(Message.RecipientType.TO, new InternetAddress(to));
        msg.setFrom(from);
        msg.setText(textOfLetter);
        Transport.send(msg);
    }
}
